﻿using Microsoft.EntityFrameworkCore;
using MinimalistFoodStreet.BackEnd.Infrastructure.Attributes;
using MinimalistFoodStreet.BackEnd.Infrastructure.Dtos.Request.Functions;
using MinimalistFoodStreet.BackEnd.Infrastructure.Dtos.Request.Tables;
using MinimalistFoodStreet.BackEnd.Infrastructure.Dtos.Response;
using MinimalistFoodStreet.BackEnd.Infrastructure.EFCore;
using MinimalistFoodStreet.BackEnd.Server.IServer.Tables;

namespace MinimalistFoodStreet.BackEnd.Server.Server.Tables
{
    [InjectClass]
    public class ChartsServer : IChartsServer
    {
        ServerRepository _serverRepository;
        public ChartsServer(DbContext dbContext)
        {
            _serverRepository = new ServerRepository(dbContext);
        }

        public async Task<ResponseApiDto> Add(RequestCreateChartsDto dto, int bid)
        {
            if (await _serverRepository.chartsEFCore.Query(d => d.CBusinessId == bid && d.CGoodId == dto.gid).AnyAsync())
            {
                return ResponseApiDto.Fail("该商品已经加入购物车了");
            }
            int affectedRows = await _serverRepository.chartsEFCore.AddAsync(new Chart { CBusinessId = bid, CGoodId = dto.gid,CGoodCount=1 });
            return ResponseApiDto.Create(affectedRows);
        }

        public async Task<ResponseApiDto> Delete(RequestIdChartsDto dto)
        {
            if (!await _serverRepository.chartsEFCore.Query(d => d.CId == dto.id).AnyAsync())
            {
                ResponseApiDto.Fail("该商品已被移除");
            }
            Chart chart = new Chart { CId = dto.id };
            int affectedRows = await _serverRepository.chartsEFCore.DeleteAsync(chart);
            return ResponseApiDto.Create(affectedRows);
        }

        public async Task<ResponseApiDto> Decrease(RequestIdChartsDto dto)
        {
            // 不查询直接更新
            var affectedRows = await _serverRepository.chartsEFCore.Query()
    .Where(d => d.CId == dto.id && d.CGoodCount > 1)
    .ExecuteUpdateAsync(setters => setters.SetProperty(x => x.CGoodCount, x => x.CGoodCount - 1));
            return ResponseApiDto.Create(affectedRows,failMessage:"商品数量不能小于1");
        }

        public async Task<ResponseApiDto> Increase(RequestIdChartsDto dto)
        {
            // 不查询直接更新
            var affectedRows = await _serverRepository.chartsEFCore.Query()
    .Where(d => d.CId == dto.id && d.CGoodCount < 99999)
    .ExecuteUpdateAsync(setters => setters.SetProperty(x => x.CGoodCount, x => x.CGoodCount + 1));
            return ResponseApiDto.Create(affectedRows,failMessage:"最大数量不能超过99999");
        }

        public async Task<ResponseApiDto> SetChartGoodCount(RequestSetChartGoodCount dto)
        {
            if (dto.count < 1)
            {
                ResponseApiDto.Fail("商品数量不能小于1");
            }
            if (dto.count > 99999)
            {
                ResponseApiDto.Fail("最大数量不能超过99999");
            }
            // 不查询直接更新
            var affectedRows = await _serverRepository.chartsEFCore.Query()
    .Where(d => d.CId == dto.id)
    .ExecuteUpdateAsync(setters => setters.SetProperty(x => x.CGoodCount, x => dto.count));
            return ResponseApiDto.Create(affectedRows);
        }

        public async Task<ResponseApiDto> GetCartList(int bid)
        {
            var data = await _serverRepository.chartsEFCore.Query(d => d.CBusinessId == bid).GroupBy(d => new { d.CGood.GSupplierId, d.CGood.GSupplier.SShopName }).Select(d => new ResponseGroupCartsDto
            {
                sid = d.Key.GSupplierId,
                sname = d.Key.SShopName,
                charts = d.Select(d => new ResponseCartsDto
                {
                    id = d.CId,
                    count = d.CGoodCount,
                    good = new ResponseGoodList
                    {
                        id = d.CGood.GId,
                        name = d.CGood.GName,
                        photo = d.CGood.GPhoto,
                        price = d.CGood.GPrice,
                        spec = d.CGood.GSpecs,
                        promotePrice = d.CGood.Promotions.Where(e => e.PEndTime > DateTime.Now).FirstOrDefault() != null ?
                       d.CGood.Promotions.Where(e => e.PEndTime > DateTime.Now).FirstOrDefault().PDiscountPrice : 0,
                        saleCout = d.CGood.OrderDetails.Where(e => e.OdOrder.OCompleteTime != null).Sum(e => e.OdCount)
                    }
                }).ToList()
            }).ToListAsync();
            return ResponseApiDto.Ok("数据已获取", data);
        }

    }
}
